﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model.Common.Infrastructure;
using CCF20Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using CCF20Model.ModelBase;
using System.Windows;
using WiseLink.RDS.Resource;

namespace CCF20Module
{
    class CustomerViewModel : ObservableObject
    {
        private CustomerModel customerModel;
        private DeviceModel deviceModel;
        public CustomerViewModel()
        {
            CreateModel();
            
        }

        private void CreateModel()
        {
            ModelFactory modelFactory = new ModelFactory();
            customerModel = (CustomerModel)modelFactory.CreateModel(ModelName.CustomerModel);
            deviceModel = (DeviceModel)modelFactory.CreateModel(ModelName.DeviceModel);
            CustomerGridSource = customerModel.GetCustomer();
            VehicleBrandSource = customerModel.GetVehicleBrand();
            ProvinceSource = customerModel.GetChinaProvince();
            IDSSource = customerModel.GetIDSByCity();
        }

        #region Properties
        private ObservableCollection<CustomerData> _customerGridSource;
        public ObservableCollection<CustomerData> CustomerGridSource
        {
            get
            {

                return _customerGridSource;
            }
            set
            {
                _customerGridSource = value;
                RaisePropertyChanged("CustomerGridSource");
            }
        }

        private CustomerData _currentCustomer;
        public CustomerData CurrentCustomer
        {
            get
            {

                return _currentCustomer;
            }
            set
            {
                _currentCustomer = value;
                
                RaisePropertyChanged("CurrentCustomer");
            }
        }

        private IEnumerable<string> _vehicleBrandSource;
        public IEnumerable<string> VehicleBrandSource
        {
            get
            {

                return _vehicleBrandSource;
            }
            set
            {
                _vehicleBrandSource = value;
                RaisePropertyChanged("VehicleBrandSource");
            }
        }
        private string _currentVehicleBrand;
        public string CurrentVehicleBrand
        {
            get
            {
                return _currentVehicleBrand;
            }
            set
            {
                if (_currentVehicleBrand != value)
                {
                    _currentVehicleBrand = value;
                    VehicleModelSource = customerModel.GetVehicleModelByBrand(_currentVehicleBrand);
                    RaisePropertyChanged("VehicleBrandSource");
                }
            }
        }

        private IEnumerable<string> _vehicleModelSource;
        public IEnumerable<string> VehicleModelSource
        {
            get
            {

                return _vehicleModelSource;
            }
            set
            {
                _vehicleModelSource = value;
                
                RaisePropertyChanged("VehicleModelSource");
            }
        }
        private string _currentVehicleModel;
        public string CurrentVehicleModel
        {
            get
            {
                return _currentVehicleModel;
            }
            set
            {
                if (_currentVehicleModel != value)
                {
                    _currentVehicleModel = value;
                    VehicleEngineSource = customerModel.GetVehicleEngineByBrandModel(_currentVehicleBrand, _currentVehicleModel);
                    RaisePropertyChanged("CurrentVehicleModel");
                }
            }
        }

        private DropDownItem _currentVehicleEngine;
        public DropDownItem CurrentVehicleEngine
        {
            get
            {
                return _currentVehicleEngine;
            }
            set
            {
                if (_currentVehicleEngine != value)
                {
                    _currentVehicleEngine = value;
                    VehicleType vt = customerModel.GetVehicleTypeById(_currentVehicleEngine.value);
                    CurrentCustomer.MyVehicle.VehicleType = vt;
                    CurrentCustomer.MyVehicle.VehicleTypeID = vt.VehicleTypeID;
                    RaisePropertyChanged("CurrentVehicleEngine");
                    RaisePropertyChanged("CurrentCustomer");
                }
            }
        }

        private IEnumerable<DropDownItem> _vehicleEngineSource;
        public IEnumerable<DropDownItem> VehicleEngineSource
        {
            get
            {

                return _vehicleEngineSource;
            }
            set
            {
                _vehicleEngineSource = value;

                RaisePropertyChanged("VehicleEngineSource");
            }
        }

        private DropDownItem _currentProvince;
        public DropDownItem CurrentProvince
        {
            get
            {
                return _currentProvince;
            }
            set
            {
                if (_currentProvince != value)
                {
                    _currentProvince = value;
                    CitySource = customerModel.GetChinaCityByProvince(_currentProvince.value);
                    RaisePropertyChanged("CurrentProvince");
                }
            }
        }

        private IEnumerable<DropDownItem> _provinceSource;
        public IEnumerable<DropDownItem> ProvinceSource
        {
            get
            {

                return _provinceSource;
            }
            set
            {
                _provinceSource = value;
                
                RaisePropertyChanged("ProvinceSource");
            }
        }

        private DropDownItem _currentCity;
        public DropDownItem CurrentCity
        {
            get
            {
                return _currentCity;
            }
            set
            {
                if (_currentCity != value)
                {
                    _currentCity = value;
                    RaisePropertyChanged("CurrentCity");
                    IDSSource = customerModel.GetIDSByCity(_currentCity.name);
                }
            }
        }

        private IEnumerable<DropDownItem> _citySource;
        public IEnumerable<DropDownItem> CitySource
        {
            get
            {
                return _citySource;
            }
            set
            {
                _citySource = value;
                RaisePropertyChanged("CitySource");
            }
        }

        private DropDownItem _currentIDS;
        public DropDownItem CurrentIDS
        {
            get
            {
                return _currentIDS;
            }
            set
            {
                if (_currentIDS != value)
                {
                    _currentIDS = value;
                    _currentCustomer.MyCustomer.IDS = _currentIDS.name;
                    _currentCustomer.MyCustomer.IDSID = (short)_currentIDS.value;
                    RaisePropertyChanged("CurrentIDS");
                    RaisePropertyChanged("CurrentCustomer");
                }
            }
        }

        private IEnumerable<DropDownItem> _idsSource;
        public IEnumerable<DropDownItem> IDSSource
        {
            get
            {
                return _idsSource;
            }
            set
            {
                _idsSource = value;
                RaisePropertyChanged("IDSSource");
            }
        }



        #endregion

        #region command

        

        public ICommand CmdSaveCustomer
        {
            get { return new RelayCommand(SaveCustomer, CanSaveCustomer); }
        }

        private void SaveCustomer()
        {

            if (_currentCustomer.MyDevice != null && _currentCustomer.MyDevice.IsRegistered == true)
            {
                customerModel.Save(_currentCustomer);

            }else if (eRDSZIC.DeviceRegister( _currentCustomer.MyDevice, 
                                        _currentCustomer.MyCustomer.IDS,
                                        _currentCustomer.MyCustomer.IDSID.ToString(), 
                                        _currentCustomer.MyCustomer.SN, 
                                        _currentCustomer.MyCustomer.SNPwd))
            {
                _currentCustomer.MyDevice.Sales = (string)Application.Current.Properties["CurrentUser"];
                _currentCustomer.IsReadOnly = true;
                customerModel.Save(_currentCustomer);
                SMSModel.SendShortMessage(_currentCustomer.MyVehicle, "开机", "开机通知模板");
                SMSModel.SendGPRSMessage(_currentCustomer.MyVehicle, "开机", "开机通知模板");
                
                
                deviceModel.AddDefaultMaintenanceToVehicle(_currentCustomer.MyVehicle);
                customerModel.Save(_currentCustomer);

                // refresh current customer
                CustomerData temp = _currentCustomer;
                CurrentCustomer = null;
                CurrentCustomer = temp;

                Utility.ShowMessageBox("客户注册成功");
            }
            
            
        }
        private Boolean CanSaveCustomer()
        {
            if (_currentCustomer == null || _currentCustomer.MyDevice == null) return false;
            bool hasNoError = string.IsNullOrEmpty(_currentCustomer.MyCustomer.Error) && string.IsNullOrEmpty(_currentCustomer.MyVehicle.Error);
            
            if (hasNoError && !string.IsNullOrEmpty(_currentCustomer.MyDevice.DeviceKey) && _currentCustomer.MyVehicle.VehicleType != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        
        public Action NewCustomerCreated { get; set; }
        
        public ICommand CmdCreateCustomer
        {
            get { return new RelayCommand(CreateCustomer); }
        }

        private void CreateCustomer()
        {
            CustomerData customerData = new CustomerData();
            _customerGridSource.Add(customerData);
            CustomerGridSource = _customerGridSource;
            CurrentCustomer = customerData;
            NewCustomerCreated();
        }


        public ICommand CmdDeleteCustomer
        {
            get { return new RelayCommand(DeleteCustomer, CanDeleteCustomer); }
        }

        private void DeleteCustomer()
        {
            _customerGridSource.Remove(_currentCustomer);
            CustomerGridSource = _customerGridSource;
        }
        private Boolean CanDeleteCustomer()
        {
            if (_currentCustomer != null && _currentCustomer.MyDevice != null && _currentCustomer.MyDevice.IsRegistered == true)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        public ICommand CmdGetDeviceKey
        {
            get { return new RelayCommand(GetDeviceKey, CanGetDeviceKey); }
        }

        private void GetDeviceKey()
        {
            if (_currentCustomer.MyDevice == null)
            {
                _currentCustomer.MyDevice = new Device();
                _currentCustomer.MyDevice.DeviceStatus = DeviceStatus.InStock;
                _currentCustomer.MyDevice.IsRegistered = false;

            }
            eRDSZIC.GetDeviceKey(_currentCustomer.MyDevice, _currentCustomer.MyCustomer.SN, _currentCustomer.MyCustomer.SNPwd);
            _currentCustomer.MyCustomer.Devices.Add(_currentCustomer.MyDevice);
        }
        private Boolean CanGetDeviceKey()
        {
            if (_currentCustomer == null || _currentCustomer.IsReadOnly)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
#endregion
    }
}
